package com.lanou.myapp.news;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.myapp.R;
import com.lanou.myapp.news.funnynews.FunnyNewFragment;
import com.lanou.myapp.news.gamanews.GameNewsFragment;
import com.lanou.myapp.news.movienews.MovieNewsFragment;
import com.lanou.myapp.news.recommendednews.RecommendedNewsFragment;

import java.util.ArrayList;

/**
 * Created by dllo on 16/1/10.
 */
public class NewsFragment extends Fragment {
    private NewsAdapter adapter;
    private Context context;
    private ArrayList<Fragment> datas;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tabLayout = (TabLayout) getActivity().findViewById(R.id.tabLayout);
        viewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);

        datas = new ArrayList<>();
        datas.add(new RecommendedNewsFragment());
        datas.add(new FunnyNewFragment());
        datas.add(new MovieNewsFragment());
        datas.add(new GameNewsFragment());

        adapter = new NewsAdapter(getActivity().getSupportFragmentManager(), datas, context);
        viewPager.setAdapter(adapter);
        tabLayout.setTabTextColors(Color.BLACK, Color.BLUE);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_news, null);
        return view;
    }
}
